package com.example.service;

import com.example.entity.PrescriptionItem;
import com.example.mapper.DrugMapper;
import com.example.mapper.PrescriptionItemMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 药品项目表业务处理
 **/
@Service
public class PrescriptionItemService {

    @Resource
    private PrescriptionItemMapper prescriptionItemMapper;

    @Resource
    private DrugService drugService;

    @Resource
    private PrescriptionService prescriptionService;

    /**
     * 新增
     */
    @Transactional
    public void add(PrescriptionItem prescriptionItem) {
        prescriptionItem.setTotalPrice(prescriptionItem.getQuantity() * prescriptionItem.getUnitPrice());
        drugService.downDrugCount(prescriptionItem);
        prescriptionItemMapper.insert(prescriptionItem);
    }

    /**
     * 删除
     */
    @Transactional
    public void deleteById(Integer id) {
        drugService.upDrugCount(prescriptionItemMapper.selectById(id));
        prescriptionItemMapper.deleteById(id);

    }

    /**
     * 批量删除
     */
    @Transactional
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            this.deleteById(id);
        }
    }

    /**
     * 修改
     */
    public void updateById(PrescriptionItem prescriptionItem) {
        prescriptionItemMapper.updateById(prescriptionItem);
    }

    /**
     * 根据ID查询
     */
    public PrescriptionItem selectById(Integer id) {
        return prescriptionItemMapper.selectById(id);
    }

    /**
     * 查询所有
     */
    public List<PrescriptionItem> selectAll(PrescriptionItem prescriptionItem) {
        return prescriptionItemMapper.selectAll(prescriptionItem);
    }

    /**
     * 分页查询
     */
    public PageInfo<PrescriptionItem> selectPage(PrescriptionItem prescriptionItem, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<PrescriptionItem> list = prescriptionItemMapper.selectAll(prescriptionItem);
        return PageInfo.of(list);
    }

}